//
//  NestScrollViewVC.swift
//  Example
//
//  Created by remy on 2019/12/14.
//  Copyright © 2019 com.Risac. All rights reserved.
//

class NestScrollViewVC: BaseVC {
    private lazy var nestScroll: NestScroll = {
        let nest = NestScroll()
        nest.rootOffsetYMax = subTop - 20.0
        nest.bouncesRootFirst = false
        return nest
    }()
    private lazy var rootScrollView: NestScrollView = {
        let view = NestScrollView(frame: bodyViewFrame, bgColor: FlatColor.turquoise)
        view.contentInset.top = 20.0
        return view
    }()
    // MARK: 开启多手势时,嵌套UIScrollView都滑至顶/底端后只有父层UIScrollView能继续响应拖动事件
    // MARK: 插入一层UIScrollView可改变响应顺序
    private lazy var wrapScrollView: UIScrollView = UIScrollView(frame: CGRect(x: 0.0, y: subTop, width: bodyViewFrame.width, height: bodyViewFrame.height - subTop))
    private lazy var subScrollView: RisacScrollView = {
        let view = RisacScrollView(frame: wrapScrollView.bounds, bgColor: FlatColor.wetAsphalt)
        view.alwaysBounceVertical = true
        view.contentInset = UIEdgeInsets(top: 20.0, left: 0.0, bottom: 20.0, right: 0.0)
        return view
    }()
    private var subTop: CGFloat = 40.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        topBarView.centerItem = .text(title)
        nestScroll.nest(root: rootScrollView, sub: subScrollView)
        rootScrollView.contentSize = CGSize(width: 0.0, height: subScrollView.bottom + 1000.0)
        self.view.addSubview(rootScrollView)
        (0..<15).forEach {
            let view = UIView(frame: CGRect(x: 0.0, y: CGFloat($0) * 44.0, width: subScrollView.width, height: 44.0), bgColor: FlatColor.randomColor($0))
            subScrollView.addSubview(view)
        }
        subScrollView.contentSize = CGSize(width: 0.0, height: subScrollView.subviews.last!.bottom)
        wrapScrollView.addSubview(subScrollView)
        rootScrollView.addSubview(wrapScrollView)
    }
}
